﻿Ext.ns('suce.orden');

suce.orden.Aceptar = function (config) {

    return new Ext.Window(Ext.apply({

        title: 'Aceptar Orden',
        width: 300,
        height: 200,
        modal: true,

        initComponent: function () {
            var me = this,
                form;

            me.addEvents('update');

            me.bbar = ['->',
                { text: '<b>Aceptar</b>', handler: me.ok, scope: me, iconCls: 'tb-ok' },
                { text: 'Cancelar', handler: me.onEsc, scope: me, iconCls: 'tb-exit' }
            ];

            form = new Ext.form.Panel({
                baseCls: 'x-plain',
                bodyPadding: 10,
                fieldDefaults: { anchor: '100%' },
                items: [
                    HIDDEN('idsolicitud'),
                    HIDDEN('idflujo'),
                    HIDDEN('idversion'),
//                  NUMBER('tasa', 'Tasa', { value: 0, allowBlank: false, dec: 2, readOnly:true }),
                    NUMBER('tasa', 'Tasa', { allowBlank: false, dec: 2 }),
                    TEXT('docnumero', 'Numero Doc', 200, { readOnly: true }),
                    TEXT('codformato', 'Formato', 200, { readOnly: true })
                  ]
            });

            me.items = [form];
            Ext.Window.prototype.initComponent.call(me);
            me.form = form.form;
        },

        show: function (p) {
            var me = this;
            Ext.Window.prototype.show.call(me);
                me.form.reset();
//            if (me.autoLoadTasa === false) {
//                me.form.setValues(p);
//            } else {
                callServer('gestor/servicio.ashx', { C: 'ORDEN', S: 'GETTASA', idsolicitud: p.idsolicitud, idversion: p.idversion }, function (v, j) {
                    p.tasa = j.tasa;
                    me.form.setValues(p);
                }, me);

//            }

        },

        ok: function () {
            var me = this,
                p = Ext.apply({ C: me.C, S: me.S }, me.form.getValues());
            if (!ISVALID(me.form)) return;
            postServer('gestor/servicio.ashx', p, function (v) {
                me.fireEvent('update', p);
                me.onEsc();
            }, me);
        }

    }, config));
};


suce.orden.Subsanar = function (config) {

    return new Ext.Window(Ext.apply({

        title: 'Subsanar Orden',
        width: 450,
        height: 210,
        modal: true,
        layout: 'border',
        closable: false,

        initComponent: function () {
            var me = this,
                form;

            me.addEvents('update');

            me.bbar = ['->',
                { text: '<b>Enviar</b>', handler: me.ok, scope: me, iconCls: 'tb-ok' },
                { text: 'Cancelar', handler: me.cancelar, scope: me, iconCls: 'tb-exit' }
            ];

            form = new Ext.form.Panel({
                region: 'center',
                height: 200,
                baseCls: 'x-plain',
                bodyPadding: 10,
                fieldDefaults: { anchor: '100%' },
                items: [
                    HIDDEN('idsubsanacion'),
                    HIDDEN('idsolicitud'),
                    HIDDEN('idversion'),
                    HIDDEN('rutaadj'),
                    HIDDEN('idactividad'),
                    HIDDEN('monttasa'),
                    HIDDEN('plazo'),
                    TEXT('snumorden', 'Nro Orden', 200, { readOnly: true }),
                    TEXTAREA('descsubsanacion', 'Comentario', 2000, { anchor: '100%' })
                ]
            });

            me.items = [form];
            Ext.Window.prototype.initComponent.call(me);
            me.form = form.form;
        },
        
        show: function (p) {
            var me = this;
            Ext.Window.prototype.show.call(me);
            me.form.setValues(p);
            me.form.ff('idactividad').setValue('N11');
            me.form.ff('monttasa').setValue(0);
            me.form.ff('plazo').setValue(0);
            //callServer('gestor/servicio.ashx', { C: 'SUBSANACION', S: 'CREAR', idsolicitud: p.idsolicitud, idversion: p.idversion }, function (v) {
            //  v = Ext.decode(v);
            //  me.form.setValues(v);
            //}, me);
        },


        ok: function () {
            var me = this,
                p = Ext.apply({ C: 'SUBSANACION', S: 'GENERAR' }, me.form.getValues());
            if (!ISVALID(me.form)) return;
            postServer('gestor/servicio.ashx', p, function (v) {
                me.fireEvent('update', p);
                me.onEsc();
            }, me);
        },

        cancelar: function () {
            var me = this;
            //p = { C: 'SUBSANACION', S: 'CANCELAR', idsubsanacion: me.form.ff('idsubsanacion').getValue() };
            //postServer('gestor/servicio.ashx', p, function (v) {
            //  me.onEsc();
            //}, me);
            me.onEsc();
        }


    }, config));
};



suce.N15 = function (config) {

    return new Ext.Window(Ext.apply({

        title: 'Notificar Rechazo de la MTO',
        width: 450,
        height: 190,
        layout: 'border',
        modal: true,

        initComponent: function () {
            var me = this,
                form;

            me.addEvents('update');

            me.bbar = ['->',
                { text: '<b>Rechazar</b>', handler: me.ok, scope: me, iconCls: 'tb-ok' },
                { text: 'Cancelar', handler: me.onEsc, scope: me, iconCls: 'tb-exit' }
            ];

            form = new Ext.form.Panel({
                region: 'center',
                height: 150,
                baseCls: 'x-plain',
                bodyPadding: 10,
                fieldDefaults: { anchor: '100%' },
                items: [
                    HIDDEN('idsolicitud'),
                    HIDDEN('idversion'),
                    TEXT('numorden', 'Nro Orden', 20, { allowBlank: false, readOnly: true }),
                    TEXTAREA('errores', 'Errores', 4000, { allowBlank: false, anchor: '100%', value: 'Corriga su MTO de acuerdo a los errores indicados' })
                ]
            });

            me.items = [form];
            Ext.Window.prototype.initComponent.call(me);
            me.form = form.form;
        },

        show: function (p) {
            var me = this;
            Ext.Window.prototype.show.call(me);
            me.form.setValues(p);
        },

        ok: function () {
            var me = this,
                p = Ext.apply({ C: 'N', S: 'N15' }, me.form.getValues());
            if (!ISVALID(me.form)) return;
            postServer('gestor/servicio.ashx', p, function (v) {
                me.fireEvent('update', p);
                me.onEsc();
            }, me);
        }


    }, config));
};


suce.N17 = function (config) {

    return new Ext.Window(Ext.apply({

        title: 'Notificar Subsanación de SUCE',
        width: 450,
        height: 380,
        layout: 'border',
        modal: true,

        initComponent: function () {
            var me = this,
                form;

            me.addEvents('update');

            me.bbar = ['->',
                { text: '<b>Notificar</b>', handler: me.ok, scope: me, iconCls: 'tb-ok' },
                { text: 'Cancelar', handler: me.onEsc, scope: me, iconCls: 'tb-exit' }
            ];

            form = new Ext.form.Panel({
                region: 'center',
                baseCls: 'x-plain',
                bodyPadding: 10,
                fieldDefaults: { anchor: '100%' },
                items: [
                    HIDDEN('idsolicitud'),
                    HIDDEN('idversion'),
                    TEXT('numorden', 'Nro Orden', 20, { allowBlank: false, readOnly: true }),
                    HBOX([
                        NUMBER('tasa', 'Tasa', { allowBlank: false, value: 0, min: 0}),
                        NUMBER('plazo', 'Plazo', { allowBlank: false, value: 2, min: 0})
                    ]),
                    TEXTAREA('texto', 'Texto', 4000, { allowBlank: false, anchor: '100%'})
                ]
            });

            me.gridA = new suce.upload.Grid({
                title: 'Adjuntos',
                region: 'south',
                height: 170,
                split: true,
                numesolicitud: me.numesolicitud,
                folder: 'NS' + me.idsubsanacion,
                readOnly: false
            });

            me.items = [form, me.gridA];
            Ext.Window.prototype.initComponent.call(me);
            me.form = form.form;
        },

        show: function (p) {
            var me = this;
            Ext.Window.prototype.show.call(me);
            me.form.setValues(p);
        },

        ok: function () {
            var me = this,
                p = Ext.apply({ C: 'N', S: 'N17' }, me.form.getValues());
            p.idsubsanacion = me.idsubsanacion;
            if (!ISVALID(me.form)) return;
            postServer('gestor/servicio.ashx', p, function (v) {
                me.fireEvent('update', p);
                me.onEsc();
            }, me);
        },

        reload: function () {
            var me = this;
            callServer('solicitud.ashx', { C: 'SUBSANACION', S: 'GETBYID', idsubsanacion: me.idsolicitud, idversion: me.idversion }, function (v) {
                v = Ext.decode(v);
                //me.numesolicitud = v.form.numesolicitud;
                me.form2.setValues(v.form);
                me.gridP.store.loadData(v.productos);
                me.gridSub.store.loadData(v.subsanaciones);
                //me.gridA.store.loadData(v.adjuntos);
            }, me);
        }
    }, config));
};

suce.N19 = function (config) {

    return new Ext.Window(Ext.apply({

        title: 'Notificar Rechazo de la MTS',
        width: 450,
        height: 350,
        layout: 'border',
        modal: true,

        initComponent: function () {
            var me = this,
                form;

            me.addEvents('update');

            me.bbar = ['->',
                { text: '<b>Rechazar</b>', handler: me.ok, scope: me, iconCls: 'tb-ok' },
                { text: 'Cancelar', handler: me.onEsc, scope: me, iconCls: 'tb-exit' }
            ];

            form = new Ext.form.Panel({
                region: 'center',
                baseCls: 'x-plain',
                bodyPadding: 10,
                fieldDefaults: { anchor: '100%' },
                items: [
                    HIDDEN('idsolicitud'),
                    HIDDEN('idversion'),
                    TEXT('numorden', 'Nro Orden', 20, { allowBlank: false, readOnly: true }),
                    TEXTAREA('errores', 'Detalle del Rechazo', 4000, { allowBlank: false, anchor: '100%', value: '' })
                ]
            });

            me.gridA = new suce.upload.Grid2({
                title: 'Adjuntos',
                region: 'south',
                height: 170,
                split: true,
                numesolicitud: me.numesolicitud,
                folder: 'NS' + me.idversion,
                readOnly: false
            });

            me.items = [form, me.gridA];

            Ext.Window.prototype.initComponent.call(me);
            me.form = form.form;
        },

        show: function (p) {
            var me = this;
            Ext.Window.prototype.show.call(me);
            me.form.setValues(p);
        },

        ok: function () {
            var me = this,
                p = Ext.apply({ C: 'N', S: 'N19' }, me.form.getValues());
            if (!ISVALID(me.form)) return;
            postServer('gestor/servicio.ashx', p, function (v) {
                me.fireEvent('update', p);
                me.onEsc();
            }, me);
        }


    }, config));
};


suce.N21 = function (config) {

    return new Ext.Window(Ext.apply({

        title: 'Aprobar la Modificación de Ampliación de SUCE',
        width: 300,
        height: 150,
        modal: true,
        layout: 'border',

        initComponent: function () {
            var me = this,
                form;

            me.addEvents('update');

            me.bbar = ['->',
                { text: '<b>Aprobar</b>', handler: me.ok, scope: me, iconCls: 'tb-ok' },
                { text: 'Cancelar', handler: me.onEsc, scope: me, iconCls: 'tb-exit' }
            ];

            form = new Ext.form.Panel({
                region: 'center',
                baseCls: 'x-plain',
                height: 150,
                bodyPadding: 10,
                fieldDefaults: { anchor: '-20' },
                items: [
                    HIDDEN('idsolicitud'),
                    HIDDEN('idversion'),
                    NUMBER('tasa', 'Tasa', { allowBlank: false, value: 0, min: 0 }),
                    NUMBER('dias', 'Dias', { allowBlank: false, value: 2, min: 0 })
                ]
            });

            me.items = [form];
            Ext.Window.prototype.initComponent.call(me);
            me.form = form.form;
        },

        show: function (p) {
            var me = this;
            Ext.Window.prototype.show.call(me);
            me.form.setValues(p);
        },

        ok: function () {
            var me = this,
                p = Ext.apply({ C: 'N', S: 'N21' }, me.form.getValues());
            if (!ISVALID(me.form)) return;
            postServer('gestor/servicio.ashx', p, function (v) {
                me.fireEvent('update', p);
                me.onEsc();
            }, me);
        }


    }, config));
};


suce.N66 = function (config) {

    return new Ext.Window(Ext.apply({

        title: 'Notificar Requerimiento de Pago',
        width: 500,
        height: 150,
        modal: true,

        initComponent: function () {
            var me = this,
                form;

            me.addEvents('update');

            me.bbar = ['->',
                { text: '<b>Aprobar</b>', handler: me.ok, scope: me, iconCls: 'tb-ok' },
                { text: 'Cancelar', handler: me.onEsc, scope: me, iconCls: 'tb-exit' }
            ];

            form = new Ext.form.Panel({
                region: 'north',
                height: 150,
                baseCls: 'x-plain',
                bodyPadding: 10,
                fieldDefaults: { anchor: '100%' },
                items: [
                    HIDDEN('idsolicitud'),
                    HIDDEN('idversion'),
                    NUMBER('tasa', 'Tasa', { allowBlank: false, value: 0, min: 0 }),
                    TEXT('motivo', 'Motivo', 1000, { allowBlank: false, value: 'Realice el pago correspondiente' })
                ]
            });

            me.items = [form];
            Ext.Window.prototype.initComponent.call(me);
            me.form = form.form;
        },

        show: function (idsolicitud, idversion) {
            var me = this;
            Ext.Window.prototype.show.call(me);
            me.form.ff('idsolicitud').setValue(idsolicitud);
            me.form.ff('idversion').setValue(idversion);
        },

        ok: function () {
            var me = this,
                p = Ext.apply({ C: 'N', S: 'N66' }, me.form.getValues());
            if (!ISVALID(me.form)) return;
            postServer('gestor/servicio.ashx', p, function (v) {
                me.fireEvent('update', p);
                me.onEsc();
            }, me);
        }


    }, config));
};


suce.subsanar = function (config) {

    return new Ext.Dialog(Ext.apply({

        title: 'Notificar SUCE',
        width: 650,
        height: 400,
        layout: 'border',
        modal: true,
        descsubsanacion: null,
        numesolicitud: null,
        folder: null,

        initComponent: function () {
            var me = this,
                form;

            me.addEvents('update', 'insert');
            //me.addEvents('update');

            me.bbar = ['->',
                { text: '<b>Guardar</b>', handler: me.ok, scope: me, iconCls: 'tb-save' },
                { text: 'Cancelar', handler: me.salir, scope: me, iconCls: 'tb-exit' }
            ];

            form = new Ext.form.Panel({
                region: 'center',
                baseCls: 'x-plain',
                bodyPadding: 10,
                fieldDefaults: { anchor: '100%' },
                items: [
                    HIDDEN('idsubsanacion'),
                    HIDDEN('idsolicitud'),
                    HIDDEN('idversion'),
                    HIDDEN('rutaadj'),
                    COMBO('idactividad', 'Tipo de notificacion', STORE('mant.ashx', { C: 'NOTIFICACIONES', S: 'GETLIST' }), {
                        listeners: {
                            scope: me,
                            'select': function (c, r, i) {
                                if (r) {
                                    var d = r[0].data;
                                    if (d.id !== 'N66') {
                                        me.form.ff('monttasa').setVisible(false);
                                        me.form.ff('descsubsanacion').setValue(me.descsubsanacion);
                                    } else {
                                        me.form.ff('monttasa').setVisible(true);
                                        me.form.ff('descsubsanacion').setValue('');
                                    }


                                }
                            }
                        }
                    }),
                    HBOX([
                        NUMBER('monttasa', 'Tasa', { allowBlank: false, value: 0, min: 0 }),
                        NUMBER('plazo', 'Plazo (dias)', { allowBlank: false, value: 2, min: 0 })
                    ]),
                    TEXTAREA('descsubsanacion', 'Texto', 4000, { allowBlank: false, anchor: '100%', value: '' })
                ]
            });

            me.gridA = new suce.upload.Grid2({
                title: 'Adjuntos',
                region: 'south',
                height: 170,
                split: true,
                //numesolicitud: me.numesolicitud,
                //folder: 'NS' + me.idsubsanacion,
                readOnly: false
            });

            me.items = [form, me.gridA];
            Ext.Dialog.prototype.initComponent.call(me);
            me.form = form.form;
        },

        show: function (p) {
            var me = this,
                P,
                rutaadj;
            Ext.Dialog.prototype.show.call(me);
            me.form.reset();
            me.action = p.idsubsanacion ? 'update' : 'insert';
            me.gridA.store.removeAll();
            me.numesolicitud = p.numesolicitud;
            if (me.action == 'update') {
                callServer('solicitud.ashx', { C: 'SUBSANACION', S: 'GETBYID', idsubsanacion: p.idsubsanacion }, function (v) {
                    v = Ext.decode(v);
                    me.form.setValues(v);
                    me.folder = v.rutaadj;
                    me.gridA.setParameters({ folder: v.rutaadj, numesolicitud: p.numesolicitud });
                    me.gridA.store.load({ params: { folder: v.rutaadj, numesolicitud: p.numesolicitud} });
                    me.form.ff('idactividad').disable();
                }, me);
            } else {
                me.form.ff('idsolicitud').setValue(p.idsolicitud);
                me.form.ff('idversion').setValue(p.idversion);
                rutaadj = 'NSTEMP' + p.items;
                me.form.ff('rutaadj').setValue(rutaadj);
                me.folder = rutaadj;
                me.gridA.setParameters({ folder: rutaadj, numesolicitud: p.numesolicitud });
                callServer('solicitud.ashx', { C: 'SUBSANACION', S: 'GETMESSAGE', idsolicitud: p.idsolicitud, idversion: p.idversion }, function (v) {
                    v = Ext.decode(v);
                    me.descsubsanacion = v.mensaje;
                }, me);
                me.form.ff('idactividad').enable();
            }
        },

        ok: function () {
            var me = this,
                P;
            if (!ISVALID(me.form)) return;
            if (!me.form.ff('idactividad').getValue()) {
                alert('Debe seleccionar un tipo de notificacion.');
                return;
            }
            P = Ext.apply({ C: 'SUBSANACION', S: 'UPDATE', numesolicitud: me.numesolicitud }, me.form.getValues());
            postServer('solicitud.ashx', P, function (v, j) {
                v = Ext.decode(v);
                P.idsubsanacion = v.idsubsanacion;
                P.nombact = me.form.ff('idactividad').getRawValue();
                P.rutaadj = me.folder;
                P.fechasubsanacion = Ext.Date.format(new Date(), 'd/m/Y');
                P.estado = 'En Edicion';
                if (me.fireEvent(me.action, P) !== false)
                    me.onEsc();
            }, me);
        },

        salir: function () {
            var me = this;
            if (me.action !== 'update') {
                callServer('upload.ashx', { C: 'DELETEALL', numesolicitud: me.numesolicitud, folder: me.folder }, function (v) {
                }, me);
            }
            me.onEsc();
        }

    }, config));
};

suce.orden.dr = function (config) {

    return new Ext.Window(Ext.apply({

        title: 'Datos DR',
        width: 450,
        height: 150,
        modal: true,
        layout: 'border',
        closable: false,

        initComponent: function () {
            var me = this,
                form;

            me.addEvents('update');

            me.bbar = ['->',
                { text: 'Cancelar', handler: me.cancelar, scope: me, iconCls: 'tb-exit' }
            ];

            form = new Ext.form.Panel({
                region: 'center',
                height: 150,
                baseCls: 'x-plain',
                bodyPadding: 10,
                fieldDefaults: { anchor: '100%' },
                items: [
                    TEXT('numedr', 'Nro DR', 200, { readOnly: true }),
                    TEXT('fechaemision', 'Fecha Emision', 200, { readOnly: true })
                ]
            });

            me.items = [form];
            Ext.Window.prototype.initComponent.call(me);
            me.form = form.form;
        },

        show: function (p) {
            var me = this;
            Ext.Window.prototype.show.call(me);
            callServer('solicitud.ashx', { C: 'DR', S: 'GETBYID', idsolicitud: p.idsolicitud, idversion: p.idversion }, function (v) {
              v = Ext.decode(v);
              me.form.setValues(v);
            }, me);
        },

        cancelar: function () {
            var me = this;
            me.onEsc();
        }


    }, config));
};

suce.subsanar.respuesta = function (config) {

    return new Ext.Window(Ext.apply({

        title: 'Respuesta de Subsanación de SUCE',
        width: 750,
        height: 600,
        layout: 'border',
        modal: true,

        initComponent: function () {
            var me = this,
                folder,
                numesolicitud,
                form;


            me.bbar = ['->',
                { text: 'Salir', handler: me.onEsc, scope: me, iconCls: 'tb-exit' }
            ];

            form = new Ext.form.Panel({
                region: 'center',
                baseCls: 'x-plain',
                bodyPadding: 10,
                items: [
					DISPLAY('fecharespuesta', 'Fecha Resp', { readOnly: true }),
                    TEXTAREA('descrespuesta', 'Respuesta', 4000, { readOnly: true, anchor: '100% -10' })
                ]
            });

            me.gridA = new suce.upload.Grid2({
                title: 'Adjuntos',
                region: 'south',
                height: 170,
                split: true,
                numesolicitud: me.numesolicitud,
                readOnly: true
            });

            me.items = [form, me.gridA];
            Ext.Window.prototype.initComponent.call(me);
            me.form = form.form;

            me.on({
                scope: me,
                'afterrender': function () {
                    //alert(me.numesolicitud);
                    me.gridA.store.load({ params: { folder: me.folder, numesolicitud: me.numesolicitud} });
                }
            });
        },

        show: function (p) {
            var me = this;
            Ext.Window.prototype.show.call(me);
            me.form.ff('descrespuesta').setValue(p.descrespuesta);
            me.form.ff('fecharespuesta').setValue(p.fecharespuesta);

        }

    }, config));
};